package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "资源管理")
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/list")
    @ApiOperation(value = "资源列表", notes = "资源列表")
    @ApiImplicitParam(name = "dto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"dto.parentResourceNo", "dto.resourceType"})
    public ResponseResult list(@RequestBody ResourceDto dto){
        return ResponseResult.success(resourceService.list1(dto));
    }

    @PostMapping("/tree")
    @ApiOperation(value = "资源树形", notes = "资源树形")
    @ApiImplicitParam(name = "dto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"dto.label"})
    public ResponseResult<TreeVo> tree(@RequestBody ResourceDto dto) {
        return ResponseResult.success(resourceService.tree(dto));
    }

    @PutMapping
    @ApiOperation(value = "资源添加", notes = "资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
            "resourceDto.dataState",
            "resourceDto.icon",
            "resourceDto.parentResourceNo",
            "resourceDto.requestPath",
            "resourceDto.resourceName",
            "resourceDto.resourceType",
            "resourceDto.sortNo"})
    public ResponseResult createResource(@RequestBody ResourceDto resourceDto) {
        resourceService.createResource(resourceDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation(value = "资源修改", notes = "资源修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
            "resourceDto.dataState",
            "resourceDto.icon",
            "resourceDto.parentResourceNo",
            "resourceDto.requestPath",
            "resourceDto.resourceName",
            "resourceDto.resourceType",
            "resourceDto.sortNo"})
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto) {
        resourceService.updateResource(resourceDto);
        return ResponseResult.success();
    }

    @PostMapping("/enable")
    @ApiOperation(value = "资源启用禁用", notes = "资源启用禁用")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
            "resourceDto.dataState",
            "resourceDto.parentResourceNo",
            "resourceDto.resourceNo"})
    public ResponseResult startOrStopResource(@RequestBody ResourceDto resourceDto) {
        Boolean result = resourceService.startOrStopResource(resourceDto);
        return ResponseResult.success(result);
    }

    @DeleteMapping("/{resourceNo}")
    @ApiOperation(value = "删除资源", notes = "删除资源")
    @ApiImplicitParam(name = "resourceNo", value = "要删除的资源No", required = true, dataType = "java.lang.Long")
    public ResponseResult deleteReousrceByMenuId(@PathVariable Long resourceNo) {
        resourceService.deleteReousrceByMenuId(resourceNo);
        return ResponseResult.success();
    }


    /**
     * @return 左侧菜单
     */
    @GetMapping("/menus")
    @ApiOperation(value = "左侧菜单", notes = "左侧菜单")
    public ResponseResult<List<MenuVo>> menus() {
        return ResponseResult.success(resourceService.menus());
    }


    /**
     * @return 左侧菜单
     */
    @GetMapping("/myButten")
    @ApiOperation("解决前端出错")
    public void myButten() {
        log.info("解决前端出错");
    }

}
